package com.itheima.ticket4;

import java.util.concurrent.locks.ReentrantLock;

/*
使用锁对象
一定要保证锁对象 唯一
 */
public class Ticket implements Runnable {
    private int piao = 100;
    private ReentrantLock lock = new ReentrantLock();

    @Override
    public void run() {
        //卖票


        while (true) {
            try {
                lock.lock();
                //加锁
                if (piao <= 0) {
                    break;
                } else {

                    Thread.sleep(100);

                    piao--;
                    System.out.println(Thread.currentThread().getName() + "卖了一张票 还剩" + piao + "张票");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                //释放锁
                lock.unlock();
            }

        }
    }
}
